package com.situ.shoplook.admin.api;


import com.github.pagehelper.PageInfo;
import com.situ.shoplook.common.model.Result;
import com.situ.shoplook.common.model.GoodsInfo;
import com.situ.shoplook.common.service.GoodsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping(value = "/api/v1/goodsInfo", produces = "application/json;charset=utf-8")
@Slf4j
public class GoodsInfoApi {

    private GoodsInfoService goodsInfoService;

    @Autowired
    public void setAdminService(GoodsInfoService goodsInfoService) {
        this.goodsInfoService = goodsInfoService;
    }

    /**
     * add
     */
    @PostMapping
    public Result add(@RequestBody GoodsInfo goodsInfo) {
        try {
            goodsInfoService.save(goodsInfo);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * remove
     */
    @DeleteMapping
    public Result remove(@RequestBody Integer[] ids) {
        log.info(Arrays.toString(ids));
            goodsInfoService.remove(ids);
            return Result.success();
    }
    /**
     * update
     */
    @PutMapping
    public Result update(@RequestBody GoodsInfo goodsInfo) {
        try {
            goodsInfoService.update(goodsInfo);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * findAll
     */
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "1",value = "page") Integer page,
                          @RequestParam(defaultValue = "10",value = "limit") Integer limit, GoodsInfo goodsInfo) {
        try {
            PageInfo<GoodsInfo> pageInfos = goodsInfoService.searchByPage(page, limit, goodsInfo);
            return Result.success(pageInfos);
        }catch (Exception e){
            return Result.error("无查询结果");
        }
    }

    @PutMapping("/state")
    public Result updataByState(@RequestBody GoodsInfo goodsInfo) {
        try {
            if(goodsInfo.getGoodsSellStatus() == 0){
                goodsInfo.setGoodsSellStatus(1);
            }else{
                goodsInfo.setGoodsSellStatus(0);
            }
            goodsInfoService.update(goodsInfo);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
